package com.dashboard.dto
{
	import flash.utils.describeType;
	
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	import mx.rpc.soap.WebService;
	import mx.utils.ObjectProxy;
	import mx.utils.StringUtil;
	
	public class Util
	{
		
		public static function removeNamespacesFromXml(channelXml:XML):XML
		{
			var xmlString:String;
			var xmlnsPattern:RegExp;
			var namespaceRemovedXML:String;
			var responseXML:XML;
			
			xmlString = channelXml.toXMLString();

			// define the regex pattern to remove the namespaces from the string
			xmlnsPattern = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
			
			// remove the namespaces from the string representation of the XML
			namespaceRemovedXML = xmlString.replace(xmlnsPattern, "");
			
			// set the string rep. of the XML back to real XML
			responseXML = new XML(namespaceRemovedXML);
			return responseXML;
			
		}
		// function to read the object and return a search string.
		public static function CreateSearchStringfromObject(objClass:Object):String
	    {
	    	 
			var varList:XMLList = flash.utils.describeType(objClass)..variable;
			var StrQueryString:String="";
			var varlen:int = varList.length();

			for(var i:int=0; i < varlen; i++)
			{
			//Show the name and the value 
			//Alert.show(varList[i].@name+':'+varList[i].@type.toString());
		 	StrQueryString=StrQueryString + varList[i].@name + "=" +  objClass[varList[i].@name];
				//objChannelActivitySearch[varList[i].@name] = "null";		
				if(i < varlen-1)
				{
					StrQueryString=StrQueryString + "&"
				}
			}
			//trace(varList[i].@name+':'+ objChannelActivitySearch[varList[i].@name]);		
			//	Alert.show(StrQueryString.toString());
			return StrQueryString;		

	    }
	    
	    
 		 
		public static function setPersonalizationData(userid:int,PersonalizationXML:XML):void
		{
		 if(PersonalizationXML!="")
			 {	
				if (Application.application.ObjConfigaration.isPersonalized.toLowerCase() == "false")
				{
				  	var statusHttpService:HTTPService = new HTTPService();
					statusHttpService.url = "data/ChannelTypeActivityRatingReport_1.xml";
					statusHttpService.resultFormat = "e4x";
					statusHttpService.addEventListener(FaultEvent.FAULT, onPersonalizationFaultHttpService);
					statusHttpService.addEventListener(ResultEvent.RESULT, onPersonalizationResultHttpService);
					statusHttpService.send();
				} else {
					var wsdlPath:String = Application.application.ObjConfigaration.voxURL;
					var webServ_getStateList:WebService  = new WebService();
					webServ_getStateList.loadWSDL(wsdlPath);
					webServ_getStateList.SetPersonalizationData(userid,PersonalizationXML);
					webServ_getStateList.SetPersonalizationData.resultFormat = "e4x";
					webServ_getStateList.addEventListener(ResultEvent.RESULT,onPersonalizationResultHttpService);
					webServ_getStateList.addEventListener(FaultEvent.FAULT,onPersonalizationFaultHttpService);
				}
			}
		}   
		
		private static function onPersonalizationResultHttpService(e:ResultEvent):void
		{
			
			//Alert.show(e.message.toString());
		}
		private static function onPersonalizationFaultHttpService(e:FaultEvent):void
		{
				//Alert.show(e.message.toString());
		}  
		
		public static function updateActivitySearchobject(searchstring:String, objchannelActivitySearch:ChannelActivitySearch):ChannelActivitySearch
		{
			
			var mystr:Array = new Array();
			mystr = searchstring.split("&");
			var varList:XMLList = flash.utils.describeType(objchannelActivitySearch)..variable;
			var varlen:int = varList.length();
			
			for(var x:int; x<varlen; x++)
			{
				for(var i:int=0;i<mystr.length;i++)
				{
				//	Alert.show(mystr[i].toString());
					var mychildarray:Array= new Array();
					mychildarray =mystr[i].toString().split("=");
					if(mychildarray[0].toString()== varList[x].@name)
					{
						objchannelActivitySearch[varList[x].@name] = mychildarray[1];
					}
				}
			}
			return objchannelActivitySearch;
		} 
		
		public static function updateRatingSearchobject(searchstring:String, objchannelRatingSearch:ChannelRatingSearch):ChannelRatingSearch
		{
			
			var mystr:Array = new Array();
			mystr = searchstring.split("&");
			var varList:XMLList = flash.utils.describeType(objchannelRatingSearch)..variable;
			var varlen:int = varList.length();
			
			for(var x:int; x<varlen; x++)
			{
				for(var i:int=0;i<mystr.length;i++)
				{
				//	Alert.show(mystr[i].toString());
					var mychildarray:Array= new Array();
					mychildarray =mystr[i].toString().split("=");
					if(mychildarray[0].toString()== varList[x].@name)
					{
						objchannelRatingSearch[varList[x].@name] = mychildarray[1];
					}
				}
			}
			return objchannelRatingSearch;
		} 
		 
		public static function xmlListToObjectArray(xmlList:XMLList):Array
		{
			var a:Array = new Array();
			for each(var xml:XML in xmlList)
			{
				var attributes:XMLList = xml.attributes();
				var o:Object = new Object();
				for each (var attribute:XML in attributes)
				{
					var nodeName:String = attribute.name().toString();
					var value:*;
					if (nodeName == "date")
					{
						var date:Date = new Date();
						date.setTime(Number(attribute.toString()));
						value = date;
					}
					else
					{
						value = attribute.toString();
					}
						
					o[nodeName] = value;
				}
				
				a.push(new ObjectProxy(o));
			}
			
			return a;
		}
	}
}